// pages/bdfw_fb/syzr.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*图片上传*/
    imgs: [],
    placeholder: '请选择',
    /*end*/
    array5: [
      '酒店餐饮',
      '休闲娱乐',
      '生活服务',
      '电子通讯',
      '汽车美容',
    ],
    index5: 0,
    user:null,
    city:""
  },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
      var user = wx.getStorageSync("userInfo")
      if (user) {
        var city = wx.getStorageSync("city")
        that.setData({
          user: user,
          city: city
        })
      } else {
        wx.showToast({
          title: "请先登录",
          icon: 'none',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/user/user'
          })
        }, 2000)
      }


  },

  //生意转让
  bindPickerChange5: function (e) {
    this.setData({
      index5: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //图片上传
  chooseImg: function (e) {
    let that = this;
    var imgs = that.data.imgs;
    var imgsLength = imgs.length
    wx.chooseImage({
      count: 9 - imgsLength,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var filePaths = res.tempFilePaths
        var filePathsLength = filePaths.length;
        var failNum = 0;
        for (var i = 0; i < filePathsLength; i++) {
          wx.uploadFile({
            url: app.globalData.theUrl1 + '/Samecity/Upload/uploadFile',
            filePath: filePaths[i],
            name: 'fileData',
            formData: {},
            success: (successBack) => {
              var imgs = that.data.imgs;
              var data = JSON.parse(successBack.data)
              if (data.code == 0) {
                imgs.push(data.url);
                that.setData({
                  imgs: imgs
                })
              } else {
                failNum++;
              }
            },
            fail: (failBack) => {
              failNum++;
            },
          });
        }
        wx.showToast({
          title: '总共' + filePathsLength + '张上传成功,' + failNum + '张上传失败！',
          icon: 'none'
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var imgs = this.data.imgs;
    wx.previewImage({
      current: imgs[index],
      urls: imgs
    })
  },
  fromSub(e) {
    console.log(e.detail.value)
    var that = this
    var fromData = e.detail.value
    if (fromData.industry == '') {
      wx.showToast({
        title: '请选择行业类型',
        icon: 'none'
      })
    } else if (fromData.name == '') {
      wx.showToast({
        title: '请输入联系人姓名',
        icon: 'none'
      })
    } else if (fromData.tel == '') {
      wx.showToast({
        title: '请输入联系人电话',
        icon: 'none'
      })
    } else if (fromData.explain == '') {
      wx.showToast({
        title: '请输入备注说明',
        icon: 'none'
      })
    } else if (that.data.imgs.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '发布中',
      })
      fromData.uid = that.data.user.id
      fromData.city = that.data.city
      fromData.images = that.data.imgs.join(',')
      app.wxRequest('post', '/Samecity/BusinessTransfer/business_transfer', fromData, (res) => {
        wx.hideLoading()
        if (res.data.code == 1) {
          wx.showToast({
            title: "发布成功",
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }, (err) => {
        wx.hideLoading()
        console.log(err)
      })
    }
  },

})